package com.weavernorth.humanresource;


import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Calendar;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Encoder;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Iterator;


//单项激励接口
public class HRDxjlAction implements Action {

    public static Log log = LogFactory.getLog(HRDxjlAction.class.getName());

    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        //主表数据库
        RecordSet rs = new RecordSet();
        boolean isSuccess = false;
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        //获取当前日期
        Calendar now = Calendar.getInstance();
        int day = now.get(Calendar.DAY_OF_MONTH);
        String requestid = requestInfo.getRequestid();//请求ID
        String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        bb.writeLog("人事-单项激励tablename的值是:" + tablename + "\n requestid" + requestid);
        //主表数据库字段
        String rq = "";//日期
        RecordSet mbrs = new RecordSet();//码表rs
        String mbsql = "";  //查询码表sql
        //1.获取主表单数据
        String sql = " select * from " + tablename + " where requestid =" + requestid;
        bb.writeLog("人事-单项激励接口sql的值是:" + sql + "\n");
        rs.execute(sql);
        if (rs.next()) {
            rq = rs.getString("rq");//生效日期
            bb.writeLog("人事-单项激励:日期sxrq的值是:" + rq + "\n");
        }
        //2.封装sap请求xml
        StringBuffer xml = new StringBuffer("");
        xml.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">");
        xml.append("<soapenv:Header/>");
        xml.append("<soapenv:Body>");
        xml.append("<urn:ZHRI_076>");

        //Return字段
        xml.append("<RETURN>");
        xml.append("<item>");
        xml.append("<TYPE></TYPE>");
        xml.append("<ID></ID>");
        xml.append("<NUMBER></NUMBER>");
        xml.append("<MESSAGE></MESSAGE>");
        xml.append("<LOG_NO></LOG_NO>");
        xml.append("<LOG_MSG_NO></LOG_MSG_NO>");
        xml.append("<MESSAGE_V1></MESSAGE_V1>");
        xml.append("<MESSAGE_V2></MESSAGE_V2>");
        xml.append("<MESSAGE_V3></MESSAGE_V3>");
        xml.append("<MESSAGE_V4></MESSAGE_V4>");
        xml.append("<PARAMETER></PARAMETER>");
        xml.append("<ROW></ROW>");
        xml.append("<FIELD></FIELD>");
        xml.append("<SYSTEM></SYSTEM>");
        xml.append("</item>");
        xml.append("</RETURN>");
        xml.append("<T_9134>");
        //子表数据
        RecordSet dt1rs = new RecordSet();
        //1.获取子表单dt1数据
        String dt1sql = " select  * from " + tablename + "_dt1 dt1 " + " where dt1.mainid=( select id from " + tablename + " mt where mt.requestid =" + requestid + ")";
        bb.writeLog("人事-单项激励接口dt1sql的值是:" + dt1sql + "\n");
        dt1rs.execute(dt1sql);
        while (dt1rs.next()) {
            //人员id
            String strXm=Util.null2String(dt1rs.getString("xm"));

            //获取数据
            String gh = dt1rs.getString("gh");//员工编号
            //调用工资范围校验接口开始

            //zm 增加,判断工资范围是否锁定 开始
            String strGzfw=salarRangeUtil.getGzfwFromUser(strXm);

            String strLockMess = salarRangeUtil.judgeLock(strGzfw, rq);
            log.info("工资核算范围返回信息:" + strLockMess);
            if (!"".equals(strLockMess)) {
                strLockMess="工号为:"+gh+","+strLockMess+"薪资核算结束后，工资范围解除锁定，流程可以继续提交";
                requestInfo.getRequestManager().setMessagecontent(strLockMess);
                return FAILURE_AND_CONTINUE;

            }
            //zm 增加,判断工资范围是否锁定 结束



            String dxjllx = dt1rs.getString("dxjllx");//单项激励类型
            double jjje = dt1rs.getDouble("jjje");//奖金金额
            //转码
            //****************单项激励类型***************************
            //子表数据
            xml.append("<item>");
            xml.append("<PERNR>" + gh + "</PERNR>");//员工编号(子表)
            //  xml.append("<Pernr>"+gh+"</Pernr>");//员工编号(子表)

            xml.append("<ZJL_TYPE>" + dxjllx + "</ZJL_TYPE>");//单项激励类型(子表)
            // xml.append("<ZjlType>"+dxjllx+"</ZjlType>");//单项激励类型(子表)

            xml.append("<ZBETRG>" + jjje + "</ZBETRG>");//奖金金额(子表)
            // xml.append("<Zbetrg>"+jjje+"</Zbetrg>");//奖金金额(子表)

            xml.append("<ZDATE>" + rq + "</ZDATE>");//日期(主表)
            // xml.append("<Zdate>"+rq+"</Zdate>");//日期(主表)

            xml.append("</item>");


        }


        xml.append("</T_9134>");

        xml.append("</urn:ZHRI_076>");
        //xml.append("</urn:Zhri076>");

        xml.append("</soapenv:Body>");
        xml.append("</soapenv:Envelope>");
        bb.writeLog("人事-单项激励接口XML的值是:" + xml.toString());
        try {
            //3.推送数据


            String txurl = Util.null2String(bb.getPropValue("personnel-erdos", "dxtcurl"));

            String responseXml = salarRangeUtil.sendPost(txurl, xml.toString(), "utf-8");
            bb.writeLog("人事-单项激励接SAP返回的值是:" + responseXml.toString());

            //===========SAP 返回解析====================

            String strWorkcode = "";

            //xmlStr为上图格式的字符串


            String EPernr = "";//员工编码
            String testType="";
            String strMessage="";

            Document doc = null;
            try {
                doc = DocumentHelper.parseText(responseXml.toString());
                Iterator<Element> its2 = doc.selectNodes("//TYPE").iterator();
                while (its2.hasNext()) {
                    Element element = its2.next();
                     testType = element.getText();
                    //System.out.println(testType);
                    if (!"".equals(testType)) {
                        isSuccess = true;
                        bb.writeLog("推送成功testType值为:" + testType + "\n");

                        break;
                    }


                }

                //返回值失败时,则提示失败
                if ("E".equals(testType)) {
                    Iterator<Element> its3 = doc.selectNodes("//MESSAGE").iterator();
                    while (its3.hasNext()) {
                        Element element2 = its3.next();
                        strMessage = element2.getText();
                        if (!"".equals(strMessage)) {
                            break;
                        }
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            if ("S".equals(testType)) {
                bb.writeLog("提交成功!!!!\n");
                return SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
                return FAILURE_AND_CONTINUE;

            }


            //4.将员工编号需要更新表单字段
//            String sqlIp="update "+tablename+" set hth="+strWorkcode+" where requestid="+requestid;
//            rs.executeQuery(sqlIp);


            //****************更新员工编号*************************
            //RecordSet updaters = new RecordSet();

//            String updatesql = "update "+tablename+" set hth="+EPernr+" where requestid="+requestid;
//            rs.executeQuery(updatesql);


//            bb.writeLog("update员工号 sql的值:" + updatesql +"\n");
//            bb.writeLog("EPernr员工号 的值:" + EPernr +"\n");

            //******************************************


        } catch (Exception e) {
            e.printStackTrace();
            return FAILURE_AND_CONTINUE;
        }


        //return FAILURE_AND_CONTINUE;

    }
}







